# 将脚本跟你要替换的html 放在同一目录，html文件替换前最好format一下
with open('index_v1.html', 'r', encoding='utf-8' ) as f: #index.html 改为你要替换的文件名
    html_str = f.readlines()

for line_i, line in enumerate(html_str):
    if line.strip().startswith('<link'):
        a = line.split(' ')
        new = ''
        for i, s in enumerate(a):
            if s.startswith('href='):
                # href="js/jPlayer/jplayer.flat.css"
                # href="{% static  "js/jPlayer/jplayer.flat.css" %}"
                a[i] = s.replace(s.split('"')[1], "{% static \"" + s.split('"')[1] + "\" %}")
            new = new + a[i] + ' '
        html_str[line_i] = new
    elif line.strip().startswith('<script'):
        a = line.split(' ')
        new = ''
        for i, s in enumerate(a):
            if s.startswith('src='):
                a[i] = s.replace(s.split('"')[1], "{% static \"" + s.split('"')[1] + "\" %}")
            new = new + a[i] + ' '
        html_str[line_i] = new
    elif line.find('<img'):
        a = line.split(' ')
        new = ''
        for i, s in enumerate(a):
            if s.startswith('src='):
                a[i] = s.replace(s.split('"')[1], "{% static \"" + s.split('"')[1] + "\" %}")
            new = new + a[i] + ' '
        html_str[line_i] = new

new_html = ''
n = 0
for s in html_str:
    n += 1
    if n == 1:
        s = s + "{% load staticfiles %}\n"
    new_html = new_html + s

with open('index_v1.html', 'w+', encoding='utf-8') as f: # index_b.html 改成替换后新生产的文件名
    f.write(new_html)
